﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Base/PasswordHelper
* 类       名: PasswordHelper
* 功      能:  
* 时      间:  2023/6/21 10:55:20
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;

namespace SiliconSelect.Base
{
    public class PasswordHelper
    {
        static bool _isUpdating = false;
        public static string? GetPassword(DependencyObject obj)
        {
            return obj.GetValue(passwordProperty).ToString();
        }

        public static void SetPassword(DependencyObject? obj, string? value)
        {
            obj?.SetValue(passwordProperty, value);
        }

        // 附加属性,主要目的是实现数据绑定
        public static readonly DependencyProperty passwordProperty =
            DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
                new PropertyMetadata("", new PropertyChangedCallback(OnPropertyChanged)));

        // 当属性发生变化的时候
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox? password = d as PasswordBox;
            if (password != null)
            {
                password.PasswordChanged -= DealPasswordChanged;
            }
            if (!_isUpdating)
            {
                if (e.NewValue != null)
                {
                    if (password != null)
                    {
                        password.Password = e.NewValue.ToString();
                    }
                }
                else
                {
                    if (password != null)
                    {
                        password.Password = null;
                    }
                }
            }
            if (password != null)
            {
                password.PasswordChanged += DealPasswordChanged;
            }
        }

        private static void DealPasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox? password = sender as PasswordBox;
            _isUpdating = true;
            SetPassword(password, password?.Password);
            _isUpdating = false;
        }


        public static readonly DependencyProperty attachProperty =
            DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordHelper),
                new PropertyMetadata(default(bool), new PropertyChangedCallback(onAttached)));

        private static void onAttached(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox? password = d as PasswordBox;
            if (password != null)
            {
                password.PasswordChanged += DealPasswordChanged;
            }
        }

        public static bool GetAttach(DependencyObject d)
        {
            return (bool)d.GetValue(attachProperty);
        }

        public static void SetAttach(DependencyObject d, bool value)
        {
            d.SetValue(attachProperty, value);
        }
    }
}
